/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;
using System.Collections;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// Output a worksheet object on the excel document. In fact, a worksheet object with a application object is equal to a worksheet document with a excel document
	/// </summary>
	[Serializable]
	public class EQWorksheet: EQRangeBase  
	{
		private EQRangeBaseCollection mObjectCollection = new EQRangeBaseCollection();
		private string mName = "";	//Worksheet's name
		private int mFrozenRows = DefaultInt;
		private int mFrozenColumns = DefaultInt;
		private ArrayList mPageBreaks = new ArrayList();
		private bool mTemplateMode;
        private bool mIsOutput = true;
        //private 

        /// <summary>
        /// false means the contents of this worksheet won't be outputed,
        /// in other words, the corresponding worksheet of the template xls file will NOT be overwriten.
        /// </summary>
        public bool IsOutput
        {
            get { return this.mIsOutput; }
            set { this.mIsOutput = value; }
        }

		/// <summary>
		/// Get the collection of EQRangeBase objects
		/// </summary>
		internal EQRangeBaseCollection ObjectCollection
		{
			get
			{
				return this.mObjectCollection;
			}
		}


		/// <summary>
		/// Set/Get whether using template mode
		/// </summary>
		internal bool TemplateMode
		{
			get
			{
				return this.mTemplateMode;
			}
			set
			{
				this.mTemplateMode = value;
			}
		}


		/// <summary>
		/// Set/Get name of worksheet
		/// </summary>
		public string Name
		{
			get
			{
				return this.mName;
			}
			set
			{
				if(value.Trim().Length == 0)
					throw new EQException("EQ1009");	//Worksheet cannot be empty

				this.mName = value.Trim();
			}
		}


		/// <summary>
		/// Set/Get the index of the frozen rows
		/// </summary>
		public int FrozenRows
		{
			get
			{
				return this.mFrozenRows;
			}
			set
			{
				this.mFrozenRows = EQKit.CheckPosition(value);
			}
		}


		/// <summary>
		/// Set/Get the index of the frozen columns
		/// </summary>
		public int FrozenColumns
		{
			get
			{
				return this.mFrozenColumns;
			}
			set
			{
				this.mFrozenColumns = EQKit.CheckPosition(value);
			}
		}


		/// <summary>
		/// Set page break on special row. That means, when printing, system will automaticall break a page base on page breaks.
		/// You can add several page breaks on your demands.
		/// </summary>
		/// <param name="rowIndex"></param>
		public void AddPageBreak(int rowIndex)
		{
			this.mPageBreaks.Add(rowIndex);
		}

		/// <summary>
		/// Add object relative to graph, text, table and otherwise except worksheet into the current object
		/// </summary>
		/// <param name="objEQRangeBase"></param>
		public void Add(EQRangeBase objEQRangeBase)
		{
			if(objEQRangeBase.GetType() == typeof(EQWorksheet))
				throw new EQException("EQ1010");
			this.mObjectCollection.Add(objEQRangeBase);
		}


		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="worksheetName">Name of worksheet</param>
		public EQWorksheet(string worksheetName)
		{
			if(worksheetName == null || worksheetName.Trim().Length == 0)
				throw new EQException("EQ1040");
			this.mName = worksheetName;
		}


		/// <summary>
		/// Output this object on the excel document. This object repesents a worksheet in the excel
		/// </summary>
		/// <param name="objWorksheet"></param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			objWorksheet.Name = this.Name;

			this.DoBeforeOperationEvent(objWorksheet);	//Event

			//Frozen rows and columns
			if(this.FrozenRows!=DefaultInt || this.FrozenColumns!=DefaultInt)
			{
				objWorksheet.Activate();
				if(this.FrozenRows!=DefaultInt)
					objWorksheet.Application.ActiveWindow.SplitRow = this.FrozenRows;
				if(this.FrozenColumns!=DefaultInt)
					objWorksheet.Application.ActiveWindow.SplitColumn = this.FrozenColumns;

				objWorksheet.Application.ActiveWindow.FreezePanes = true;
			}

			//Add page breaks
			if(this.mPageBreaks.Count > 0)
			{
				foreach(object obj in this.mPageBreaks)
					objWorksheet.HPageBreaks.Add(objWorksheet.Rows[(int)obj, this.Missing]);	
			}

			//Output objects relative to text and graph relative on the excel document
			this.mObjectCollection.Output(objWorksheet);

			this.DoAfterOperationEvent(objWorksheet);	//Event
		}
	}
}
